package com.gqz.aircraftwar.mvp.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.gqz.aircraftwar.App
import kotlin.math.min
import android.graphics.Bitmap
import android.view.animation.*
import androidx.core.graphics.minus
import com.gqz.aircraftwar.R
import java.nio.Buffer
import java.nio.ByteBuffer
import java.nio.IntBuffer
import kotlin.math.abs
import kotlin.system.measureTimeMillis


/**
 * 测试敌机路径效果用
 *
 * @author gqz
 * @data 2020/3/25
 */

class TestPath : View {
    private val TAG: String = "TestPath-->"

    var paint = Paint()
    var path = Path()
    var pm = PathMeasure()
    var cx = 0f
    var cy = 0f
    var mMatrix = Matrix()
    var pos = floatArrayOf(0f, 0f)
    var tan = floatArrayOf(0f, 0f)

    var increment = 0f

    var bmp: Bitmap
    var bmp2: Bitmap
    var judgeBmp: Bitmap
    var judgeCanvas: Canvas

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        paint.isAntiAlias = true
        paint.isDither = true
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 1f
        paint.color = Color.BLACK
        bmp = BitmapFactory.decodeResource(resources, R.drawable.plane)
        bmp2 = Bitmap.createBitmap(bmp)
        judgeBmp = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888)
        judgeCanvas = Canvas(judgeBmp)
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var wsize = MeasureSpec.getSize(widthMeasureSpec)
        var hsize = MeasureSpec.getSize(heightMeasureSpec)
        //使其为方形
        var size = min(wsize, hsize)
        setMeasuredDimension(size, size)
        cx = size / 2f
        cy = cx
        //在init中创建bmp导致无法在preview中查看自定义view
        bmp = BitmapFactory.decodeResource(
            App.instance.resources, com.gqz.aircraftwar.R.drawable.plane
        )
        var m = Matrix()
        m.postScale(0.3f, -0.3f)
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.width, bmp.height, m, false)
    }

    var adi = LinearInterpolator()
//    var adi = AccelerateInterpolator()
//    var adi = DecelerateInterpolator()
//    var adi = AccelerateDecelerateInterpolator()

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (canvas != null) {
//            setLayerType(LAYER_TYPE_SOFTWARE, null)//关闭硬件加速
            canvas.drawColor(Color.parseColor("#ffffff"))

            var pm = PathMeasure()

            increment += 0.00075f
            if (increment >= 1f) {
                increment = 0f
            }
            var a = abs(adi.getInterpolation(increment))
            var path = Path()

            //路线1（X型）  0.001f
//            var startPos = PointF(width * 9f / 10f, -30f)
//            var endPos = PointF(-200f, height * 6f / 10f)
//            var ctrlPos = PointF(abs(startPos.x + endPos.x), abs(startPos.y + endPos.y))
//            path.moveTo(startPos.x, startPos.y)
//            path.quadTo(ctrlPos.x, ctrlPos.y, endPos.x, endPos.y)
//
//            path.rLineTo(0f, -height * 7f / 10f)
//            path.rLineTo(width / 5f, 0f)
//            path.quadTo(width - ctrlPos.x, ctrlPos.y, width + 100f, height * 6f / 10f)

            //路线2（S型）  0.0005f
//            var ctrlY = 200f
//            var ctrlY2 = ctrlY * 2
//            var ctrlX = ctrlY * 3 / 4
//            path.moveTo(-100f, 100f)
//            path.lineTo(ctrlY, 100f)
//            for (num in 1..2) {
//                path.rLineTo(width - ctrlY2, 0f)
//                path.rCubicTo(ctrlX, 0f, ctrlX, ctrlY, 0f, ctrlY)
//                path.rLineTo(-width + ctrlY2, 0f)
//                path.rCubicTo(-ctrlX, 0f, -ctrlX, ctrlY, 0f, ctrlY)
//            }
//            path.rLineTo(width.toFloat() + ctrlY2, 0f)

            //路线3（U型）
//            var margin = 100f
//            var wide = width - margin * 2f
//            path.moveTo(margin, -margin)
//            path.lineTo(margin, 0f)
//            path.rLineTo(0f, height * 2f / 4f)
//            path.rCubicTo(
//                0f, wide * 2 / 3, wide, wide * 2 / 3,
//                wide, 0f
//            )
//            path.rLineTo(0f, -height * 3f / 4f)

            //路线4 （Boss）
//            path.moveTo(width / 9f, height / 10f)
//            path.rLineTo(width * 7f / 9f, 0f)
//            path.rLineTo(0f, height / 10f)
//            path.rLineTo(-width * 3f / 9f, 0f)
//            path.rLineTo(0f,height/2f)
//            path.rLineTo(0f,-height/2f)
//            path.rLineTo(-width*4f/9f,0f)
//            path.close()


//            pm.setPath(path, false)
//            for (num in 0..4) {
//                pm.getPosTan(pm.length * a - num * 100, pos, tan)
//                canvas.drawBitmap(bmp, pos[0] - bmp.width / 2, pos[1] - bmp.height / 2, paint)
//            }
        }

        //飞机圆形进度条
//        canvas?.rotate(-90f, cx, cy)
//        path.reset()
//        path.addCircle(cx, cy, cx * 0.75f, Path.Direction.CW)
//        pm.setPath(path, false)
//        paint.color = Color.BLACK
//        canvas?.drawPath(path, paint)

//        increment += 0.01f
//        if (increment >= 1f) {
//            increment = 0f
//        }
//        var a = adi.getInterpolation(increment)

//        pm.getPosTan(pm.length * a, pos, tan)
//        mMatrix.reset()
//        mMatrix.postTranslate(pos[0] - bmp.width / 2, pos[1] - bmp.height / 2)
//        var degree = atan2(tan[1], tan[0]) * 180f / PI
//        mMatrix.postRotate(degree.toFloat() + 90f, pos[0], pos[1])
//        canvas?.drawBitmap(bmp, mMatrix, paint)
//        paint.color = Color.RED
//        canvas?.drawCircle(cx, cy, 3f, paint)
//        invalidate()
    }

}
